package com.cyq.stock.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.cyq.stock.service.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/flow")
//    @SentinelResource(value = "flow", blockHandler = "flowBlockHandler")
    public String flow() {
        System.out.println("flow!");
        return "正常访问";
    }

    @GetMapping("/flowThread")
//    @SentinelResource(value = "flowThread", blockHandler = "flowBlockHandler")
    public String flowThread() throws InterruptedException {
        Thread.sleep(5000);
        return "正常访问";
    }

    @GetMapping("/err")
    public String err() throws InterruptedException {
        int i = 1 / 0;
        return "hello";
    }

    @GetMapping("/add")
    public String add() {
        System.out.println("下单成功!");
        return "生成订单";
    }

    @GetMapping("/get")
    public String get() {
        return "查询订单";
    }


    @Autowired
    private OrderServiceImpl orderService;

    @RequestMapping("/test1")
    public String test1() {
        return orderService.getUser();
    }

    @RequestMapping("/test2")
    public String test2() {
        return orderService.getUser();
    }


    public String flowBlockHandler(BlockException bE) {
        bE.printStackTrace();
        return "流控";
    }


    @RequestMapping("/get/{id}")
    @SentinelResource(value = "getById", blockHandler = "hotBlockHandler")
    public String getById(@PathVariable("id") String id) {
        System.out.println("正常访问");
        return "正常访问";
    }

    public String hotBlockHandler(@PathVariable("id") String id, BlockException be) {
        return "热点异常处理";
    }


}
